package com.example.administrator.testxingpiao.ui.register;

import android.os.Bundle;
import android.print.PrintAttributes;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.testxingpiao.R;
import com.example.administrator.testxingpiao.base.BaseActivity;
import com.example.administrator.testxingpiao.bean.User;
import com.example.administrator.testxingpiao.dao.UserDao;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.textview_register_name)
    EditText etName;
    @BindView(R.id.textview_register_password)
    EditText etPass;
    @BindView(R.id.button_register)
    Button btRegister;
    @BindView(R.id.textview_register_repassword)
    EditText etRePass;

    private String name = "";
    private String pass = "";
    private String rePass = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    private void initData() {
        name = etName.getText().toString().trim();
        pass = etPass.getText().toString().trim();
        rePass = etRePass.getText().toString().trim();
    }

    @OnClick(R.id.button_register)
    public void onClick() {
        initData();
        if (name.length() > 0 && pass.length() > 0 && rePass.length() > 0) {
            User user = new User();
            user.setName(name);
            user.setPwd(pass);
            if(pass.equals(rePass)){
                int add = UserDao.add(user, this);
                log("add=" + add);
                if (add != -1) {
                    Toast.makeText(RegisterActivity.this, "用户名注册成功", Toast.LENGTH_SHORT).show();
                } else {
                    log("失败了");
                }
            }else {
                Toast.makeText(RegisterActivity.this, "用户名密码不一致", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(RegisterActivity.this, "为空！", Toast.LENGTH_SHORT).show();
        }
    }

    public void log(String msg) {
        Log.i("TAG", msg);
    }
}
